/*
 *表情
 */
(function($) {
    var em = [
                {'id':1,'phrase':'[对眼]','url':'1.png'},{'id':2,'phrase':'[可爱]','url':'2.png'},
                {'id':3,'phrase':'[甜笑]','url':'3.png'},{'id':4,'phrase':'[可怜]','url':'4.png'},
                {'id':5,'phrase':'[憨笑]','url':'5.png'},{'id':6,'phrase':'[调皮]','url':'6.png'},
                {'id':7,'phrase':'[微笑]','url':'7.png'},{'id':8,'phrase':'[闭嘴]','url':'8.png'},
                {'id':9,'phrase':'[呲牙]','url':'9.png'},{'id':10,'phrase':'[大眼]','url':'10.png'},
                {'id':11,'phrase':'[偷笑]','url':'11.png'},{'id':12,'phrase':'[眨眼]','url':'12.png'},
                {'id':13,'phrase':'[得意]','url':'13.png'},{'id':14,'phrase':'[吐舌]','url':'14.png'},
                {'id':15,'phrase':'[卖萌]','url':'15.png'},{'id':16,'phrase':'[坏笑]','url':'16.png'},
                {'id':17,'phrase':'[疑惑]','url':'17.png'}
            ];

    /*
     *表情文字符号转换为html格式
     * @param string： str
     * @retrun string : _str
     */
    window.faceToHtml = function(str) {
        var _regx = /\[[\u4e00-\u9fa5]*\w*\]{1}/g;
        //正则查找“[]”格式
        var _ret = str.match(_regx);
        var _str = str;
        if (_ret) {
            for (i = 0; i < _ret.length; i++) {
                for (n = 0; n < em.length; n++) {
                    if (em[n].phrase == _ret[i]) {
                        var t = "<img src=' /images/emotions/" + em[n].url + "' />";
                        _str = _str.replace(_ret[i], t);
                        break;
                    }
                }
            }
        }
        return _str;
    };
    //格式化名字 名字前两位
    window.formatName = function (str) {
        var _reg= new RegExp("^\.{2}");
        var _name = str.match(_reg);
        if(_name){
            return _name;
        }else{
            return (str);
        }
    }

    /*
     *@params: oIpt-输入框（jquery对象）
     *         emotions： 表情
     */
    $.fn.extend({
        faceedit : function(options) {
            var defaults = {
                oIpt : '', //输入
                emotions : em //表情，id表情排序号 phrase表情使用的替代短语url表情文件名
            };
            var options = $.extend(defaults, options);
            return this.each(function() {
                var oSel = $(this);
                var oIpt = options.oIpt;
                var oContainer =  oSel.next();
                //展开表情
                oSel.on("click", function(e) {
                    e.stopPropagation();
                    oIpt = $(this).parents(".js_form").find(".js_ipt");
                    oContainer.fadeIn();
                    var _html = "";
                    for (i = 0; i < options.emotions.length; i++) {
                        _html += '<li text=' + options.emotions[i].phrase + ' type=' + i + '><img title=' + options.emotions[i].phrase + ' src="/Images/emotions/' + options.emotions[i].url + '"  style="cursor:pointer; position:relative;"   /></li>';
                    }
                    oContainer.find(".list ul").html(_html);
                });
                //选择表情
                oContainer.on("click","li",function(e) {
                        var _txt = $(this).attr("text");
                            _txt =oIpt.val() ? oIpt.val()+_txt : _txt;
                        oIpt.val(
                            _txt
                        );
                        oContainer.fadeOut();
                    });
                //关闭表情
                oContainer.find(".close").on("click", function() {
                        oContainer.fadeOut();
                    });

            });
        }
    })
})(jQuery);
